package cn.krund.lyc.hotlerobot.volumemanage;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.media.AudioManager;
import android.support.annotation.NonNull;
import android.support.annotation.StyleRes;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.SeekBar;

import cn.krund.lyc.hotlerobot.R;

/**
 * @包名: cn.krund.lyc.hotlerobot.volumemanage
 * @作者: 刘育成
 * @时间: 2018/3/27  17:16
 * @org: 克路德人工智能
 * @描述: TODO
 * @fileName: VolumeDialog
 */

public class VolumeDialog extends Dialog {


    public VolumeDialog(Context context, int themeResId) {
        super(context, themeResId);
    }

    /**
     * 设置 Dialog的大小
     *
     * @param x 宽比例
     * @param y 高比例
     */
    public void setDialogWindowAttr(double x, double y, Activity activity) {
        if (x < 0 || x > 1 || y < 0 || y > 1) {
            return;
        }
        Window window = this.getWindow();
        WindowManager.LayoutParams lp = window.getAttributes();
        WindowManager manager = activity.getWindowManager();
        DisplayMetrics outMetrics = new DisplayMetrics();
        manager.getDefaultDisplay().getMetrics(outMetrics);
        int width = outMetrics.widthPixels;
        int height = outMetrics.heightPixels;
        lp.gravity = Gravity.CENTER;
        lp.width = (int) (width * x);
        lp.height = (int) (height * y);
        this.getWindow().setAttributes(lp);
    }


    public static class Builder {
        private Context context;
        private VolumeDialog mDialog;
        private View layout;
        private SeekBar seekbar_music;

        private AudioManager mAudioManager;

        public Builder(Context context) {
            this.context = context;
            mDialog = new VolumeDialog(context, R.style.selectDialog);
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            layout = inflater.inflate(R.layout.dialog_volume_search, null);
            seekbar_music = (SeekBar) layout.findViewById(R.id.seekbar_music);

            mDialog.addContentView(layout, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        }

        private VolumeDialog create() {
            mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
            getAndsetVolume();
            seekbar_music.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
                @Override
                public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                    if (fromUser) {//首先判断是否是用户用户点击滑块，因为程序初始化时会设置滑块位置
                        switch (seekBar.getId()) {
                            case R.id.seekbar_music:
                                int music = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
                                if (progress < music) {//调小音量
                                    for (int i = 0; i < music - progress; i++) {
                                        //setVolume = true;//广播相关
                                        mAudioManager.adjustStreamVolume(AudioManager.STREAM_MUSIC, AudioManager.ADJUST_LOWER, AudioManager.FLAG_VIBRATE);
                                    }
                                } else if (progress > music) {//调大音量
                                    for (int i = 0; i < progress - music; i++) {
                                        //setVolume = true;//广播相关
                                        mAudioManager.adjustStreamVolume(AudioManager.STREAM_MUSIC, AudioManager.ADJUST_RAISE, AudioManager.FLAG_VIBRATE);
                                    }
                                }
                                break;
                            default:
                                break;
                        }
                    }
                }

                @Override
                public void onStartTrackingTouch(SeekBar seekBar) {

                }

                @Override
                public void onStopTrackingTouch(SeekBar seekBar) {

                }
            });
            mDialog.setOnDismissListener(new OnDismissListener() {
                @Override
                public void onDismiss(DialogInterface dialog) {

                }
            });
//            mDialog.setContentView(layout);
            //用户可以点击手机Back键取消对话框显示
            mDialog.setCancelable(true);
            //用户能通过点击对话框之外的地方取消对话框显示
            mDialog.setCanceledOnTouchOutside(true);
            return mDialog;
        }

        private void getAndsetVolume() {
//媒体声音
            int max_music = mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
            int cur_music = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
            //Log.d("demo", "music_max:"+max_music +", cur"+cur_music);
            seekbar_music.setMax(max_music);
            seekbar_music.setProgress(cur_music);
        }

        public VolumeDialog show() {
            create();
            mDialog.show();
            return mDialog;
        }
    }
}
